Data Types, Operators, Input and Functions




Data Storage Types:


Hello again. Welcome to installment 2 of my tutorial series. In this section, I will show you how to use variables to store data, operators used in C, ways to get data from the user and programming structures called functions which are the backbone of most large programs. If you're not familiar with the foundations of a C program, reread the previous tutorial until you're confident of these concepts. Anyway, on to the tutorial.
Take a look at this program.

#include <stdio.h>

int main()
{
	int i = 5;
	char c = 'c';
	float pi = 3.14;
	return (1);
}

These values (i, c, pi) are called variables, because their value can be changed (varied) as needed. They can be assigned a value at declaration (when you state that they exist) or later on, like this:
int i;
i = 5;

Any variable that you use in your program must be defined before you can use it. There are many data types you can use in your programs. Most of the common ones are included in the table below:

TypeTranslationExamples
intinteger-427, 5000
shortshort integer-27, 241
longlong integer-125000, 251425
unsignedunsigned integer26, 15842
charcharacter'a', 'q'
unsigned charunsigned character12, 255
floatfloating point number3.14, 7.95
doubledouble precision number3.1415926, 6.245187
boolboolean valuetrue, false


When assigning a char a value such as 'z' or 'F' you must use the single quotes around the character. Integer values (int, short int or unsigned char) are often used in fast applications rather than floats or doubles due to the fact that floats and doubles are extremely slow in calculations. This will not be important to you now, but might be later.

Mathematical Operators:


Next we will examine the operators used in C/C++. A table of operators used is included following this. You will not immediately use all or even most of these, but eventually you probably will use most of them.

OperatorTranslationUses
=Assignment OperatorAssign a value to a variable
+ Addition OperatorAdds two values together
-Subtraction OperatorSubtracts 1 number from another
*Multiplication OperatorMultiplies two values together
/Division OperatorDivides 1 number by another
==Conditional Equality OperatorTest if variables are equal
>Greater Than OperatorTest if 1 variable is more than another
<Less Than OperatorTest if 1 variable is less than another
&&Logical And OperatorUsed to combine two or more statements together
||Logical Or OperatorSame as Logical And Operator
&Bitwise And OperatorUsed to combine two variables together
|Bitwise Or OperatorSame as above
^Bitwise Xor OperatorSame as above
<<Left Shift OperatorShift the bits of a variable to the left
>>Right Shift OperatorShift the bits of a variable to the right
~Ones Complement OperatorSwitch all bits from 0 to 1 and 1 to 0
&Reference OperatorUsed in function calls to denote the address of a variable


The only ones you'll be using for quite a while are the first 10 operators. Numbers 6 through 10 I will be explaining in tutorial 3, numbers 2 through 5 should be fairly obvious, but if you're having trouble, drop me a line. Number 1 I'll be explaining right here. The assignment operator is used to set a variable to a certain value. You've already used it several times, but you didn't know it was an actual operator. The syntax for the assignment operator is as follows:
<variable> = <value or other variable>;

Once again, don't forget the semi-colon. Three examples of the use of the assignment operator are as follows using the int, char and float types:

float f = 7.11;
int i = 5;
char c = 'g';

This should be familiar stuff by now. Just a word of caution, make sure that you assign the correct type to the variable you are assigning. Otherwise the value will be translated from one type to another to fit in the selected variable. Now, with the assignment variable clear, we will move on to user input. The way you get input from the user is the scanf() function.

Data Storage Types:


Take a look at the following example:

int i;
scanf("%i", &i);

This example will get an integer value from the user and store it in the variable i. Here is an explanation of what it is doing. In the quotes you see the symbol %i. This is the printf/scanf symbol for an integer. Use it for getting an integer variable for now, and I'll explain the printf side later on. Below is a list of all the %_ symbols used in printf()/scanf() statement.

TypePrintf/Scanf Code
integer%i
short int%hi
long int%li
unsigned int%u
char%c
string%s
float%f
double%f


These can be used in scanf and will be important in the printf function later on. Now, on to the variable. The input is stored in the variable i and you type it &i. Why? you may ask. Well, it's actually really complex, so I'll give you the shortened version. You are sending the _address_ of the variable i in the computer's memory to the scanf() function and not just the variable itself. The ampersand "&" is called the reference operator and is actually the hexadecimal address of the variable in the computer's memory. All that isn't really important, as all you need to know for know is that you need to include the ampersand before the variable name to successfully get input from the user. That's all there is to input from the user.

Now on to functions. You may have heard of functions (from me or elsewhere) or maybe of subroutines. They are essentially the same thing, only that subroutine is a general term and function is a C/C++ programming structure. We will re-examine our first program here.

#include <stdio.h>

int main()
{
	/* Print to Screen */
	printf ("Hey World, how's it going?\n");
	return (1);
}

These look familiar, don't they? Well, lets do something interesting and change it to include a small function to do the work for us.

Functions:


#include <stdio.h>

void Print()
{
	/* Print to Screen */
	printf ("Hey World, how's it going?\n");
	return;
}
int main()
{
	Print();
	return (1);
}

You may be saying, "What?? Why did he do that?" Well, hold on a second. This may not look useful now, but it will become extremely useful as your programs grow larger and larger. First, I'll explain this program line by line.

First, the line:
void Print()

This line follows the function syntax which is like this:

<return type> <function name> (<argument 1 type> <argument 1 name>, ... )


In this case, the return type was void, meaning that there is no return value. The name of the function is Print and there are no arguments (as dictated by a lack of anything inside the parentheses). Now, I'll give you a different example that uses arguments and a return type (in C only, due to the fact that you can now tell the difference).

#include <stdio.h>

int Print( int i )
{
	i = i + 5;
	/* Print to Screen */
	printf ("The value of the argument has changed\n");
	return (i);
}
int main()
{
	int number;
	/* Get a number from the user */
	scanf ("%i", &number);
	Print(number);
	return (1);
}

Now, while this is still a little impractical, it does demonstrate a small amount of the capability that functions have. Functions are one of the most versatile programming structures that C offers and I will be covering them in depth in future tutorials.

Here's the Practice tasks for this tutorial:

1. Create a program which calls one function which calls another to print a message to the user.
2. Create a program which accepts a float value and an int value from the user and stores them to variables.
3. Make the program from #2 into a function and create a program which calls that function.

Thanks for reading.